{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0040ddd4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy\n",
    "import pandas"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5f210bf",
   "metadata": {},
   "source": [
    "# Pandas层次化索引"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d46df23",
   "metadata": {},
   "source": [
    "- 创建层次化索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4e823abe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张一</th>\n",
       "      <td>79</td>\n",
       "      <td>1</td>\n",
       "      <td>13</td>\n",
       "      <td>40</td>\n",
       "      <td>23</td>\n",
       "      <td>54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张二</th>\n",
       "      <td>64</td>\n",
       "      <td>0</td>\n",
       "      <td>16</td>\n",
       "      <td>1</td>\n",
       "      <td>85</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>22</td>\n",
       "      <td>99</td>\n",
       "      <td>40</td>\n",
       "      <td>90</td>\n",
       "      <td>18</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>张四</th>\n",
       "      <td>24</td>\n",
       "      <td>25</td>\n",
       "      <td>44</td>\n",
       "      <td>27</td>\n",
       "      <td>46</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张五</th>\n",
       "      <td>33</td>\n",
       "      <td>55</td>\n",
       "      <td>92</td>\n",
       "      <td>46</td>\n",
       "      <td>19</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张六</th>\n",
       "      <td>47</td>\n",
       "      <td>29</td>\n",
       "      <td>31</td>\n",
       "      <td>53</td>\n",
       "      <td>17</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       期中          期末        \n",
       "       语文  数学  英语  语文  数学  英语\n",
       "1班 张一  79   1  13  40  23  54\n",
       "   张二  64   0  16   1  85  40\n",
       "   张三  22  99  40  90  18  80\n",
       "2班 张四  24  25  44  27  46   4\n",
       "   张五  33  55  92  46  19  74\n",
       "   张六  47  29  31  53  17  55"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 隐式构造\n",
    "data = numpy.random.randint(0,100,size=(6,6))\n",
    "index = [\n",
    "    ['1班','1班','1班','2班','2班','2班'],\n",
    "    ['张一','张二','张三','张四','张五','张六']\n",
    "]\n",
    "columns = [\n",
    "    ['期中','期中','期中','期末','期末','期末'],\n",
    "    ['语文','数学','英语','语文','数学','英语']\n",
    "]\n",
    "df = pandas.DataFrame(data=data,index=index,columns=columns)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b74e2df",
   "metadata": {},
   "source": [
    "- 多层行索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69789687",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "84e56f87",
   "metadata": {},
   "source": [
    "- 多层列索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64b39283",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "5e1a843a",
   "metadata": {},
   "source": [
    "- 索引和切片操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c84c66b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "07f98e2d",
   "metadata": {},
   "source": [
    "- 多层索引的堆叠\n",
    "    - stack()\n",
    "    - unstack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2bc252de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>期中</th>\n",
       "      <th>期末</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"9\" valign=\"top\">1班</th>\n",
       "      <th rowspan=\"3\" valign=\"top\">张一</th>\n",
       "      <th>数学</th>\n",
       "      <td>1</td>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>英语</th>\n",
       "      <td>13</td>\n",
       "      <td>54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>语文</th>\n",
       "      <td>79</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">张二</th>\n",
       "      <th>数学</th>\n",
       "      <td>0</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>英语</th>\n",
       "      <td>16</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>语文</th>\n",
       "      <td>64</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">张三</th>\n",
       "      <th>数学</th>\n",
       "      <td>99</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>英语</th>\n",
       "      <td>40</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>语文</th>\n",
       "      <td>22</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"9\" valign=\"top\">2班</th>\n",
       "      <th rowspan=\"3\" valign=\"top\">张四</th>\n",
       "      <th>数学</th>\n",
       "      <td>25</td>\n",
       "      <td>46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>英语</th>\n",
       "      <td>44</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>语文</th>\n",
       "      <td>24</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">张五</th>\n",
       "      <th>数学</th>\n",
       "      <td>55</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>英语</th>\n",
       "      <td>92</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>语文</th>\n",
       "      <td>33</td>\n",
       "      <td>46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">张六</th>\n",
       "      <th>数学</th>\n",
       "      <td>29</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>英语</th>\n",
       "      <td>31</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>语文</th>\n",
       "      <td>47</td>\n",
       "      <td>53</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          期中  期末\n",
       "1班 张一 数学   1  23\n",
       "      英语  13  54\n",
       "      语文  79  40\n",
       "   张二 数学   0  85\n",
       "      英语  16  40\n",
       "      语文  64   1\n",
       "   张三 数学  99  18\n",
       "      英语  40  80\n",
       "      语文  22  90\n",
       "2班 张四 数学  25  46\n",
       "      英语  44   4\n",
       "      语文  24  27\n",
       "   张五 数学  55  19\n",
       "      英语  92  74\n",
       "      语文  33  46\n",
       "   张六 数学  29  17\n",
       "      英语  31  55\n",
       "      语文  47  53"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将列索引变成行索引\n",
    "df.stack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f03c1e7b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"6\" valign=\"top\">1班</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">张一</th>\n",
       "      <th>期中</th>\n",
       "      <td>1</td>\n",
       "      <td>13</td>\n",
       "      <td>79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>23</td>\n",
       "      <td>54</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">张二</th>\n",
       "      <th>期中</th>\n",
       "      <td>0</td>\n",
       "      <td>16</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>85</td>\n",
       "      <td>40</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">张三</th>\n",
       "      <th>期中</th>\n",
       "      <td>99</td>\n",
       "      <td>40</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>18</td>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"6\" valign=\"top\">2班</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">张四</th>\n",
       "      <th>期中</th>\n",
       "      <td>25</td>\n",
       "      <td>44</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>46</td>\n",
       "      <td>4</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">张五</th>\n",
       "      <th>期中</th>\n",
       "      <td>55</td>\n",
       "      <td>92</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>19</td>\n",
       "      <td>74</td>\n",
       "      <td>46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">张六</th>\n",
       "      <th>期中</th>\n",
       "      <td>29</td>\n",
       "      <td>31</td>\n",
       "      <td>47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>17</td>\n",
       "      <td>55</td>\n",
       "      <td>53</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          数学  英语  语文\n",
       "1班 张一 期中   1  13  79\n",
       "      期末  23  54  40\n",
       "   张二 期中   0  16  64\n",
       "      期末  85  40   1\n",
       "   张三 期中  99  40  22\n",
       "      期末  18  80  90\n",
       "2班 张四 期中  25  44  24\n",
       "      期末  46   4  27\n",
       "   张五 期中  55  92  33\n",
       "      期末  19  74  46\n",
       "   张六 期中  29  31  47\n",
       "      期末  17  55  53"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = df.stack(level=0)\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "fec2956f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">数学</th>\n",
       "      <th colspan=\"2\" halign=\"left\">英语</th>\n",
       "      <th colspan=\"2\" halign=\"left\">语文</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>期中</th>\n",
       "      <th>期末</th>\n",
       "      <th>期中</th>\n",
       "      <th>期末</th>\n",
       "      <th>期中</th>\n",
       "      <th>期末</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张一</th>\n",
       "      <td>1</td>\n",
       "      <td>23</td>\n",
       "      <td>13</td>\n",
       "      <td>54</td>\n",
       "      <td>79</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>99</td>\n",
       "      <td>18</td>\n",
       "      <td>40</td>\n",
       "      <td>80</td>\n",
       "      <td>22</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张二</th>\n",
       "      <td>0</td>\n",
       "      <td>85</td>\n",
       "      <td>16</td>\n",
       "      <td>40</td>\n",
       "      <td>64</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>张五</th>\n",
       "      <td>55</td>\n",
       "      <td>19</td>\n",
       "      <td>92</td>\n",
       "      <td>74</td>\n",
       "      <td>33</td>\n",
       "      <td>46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张六</th>\n",
       "      <td>29</td>\n",
       "      <td>17</td>\n",
       "      <td>31</td>\n",
       "      <td>55</td>\n",
       "      <td>47</td>\n",
       "      <td>53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张四</th>\n",
       "      <td>25</td>\n",
       "      <td>46</td>\n",
       "      <td>44</td>\n",
       "      <td>4</td>\n",
       "      <td>24</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       数学      英语      语文    \n",
       "       期中  期末  期中  期末  期中  期末\n",
       "1班 张一   1  23  13  54  79  40\n",
       "   张三  99  18  40  80  22  90\n",
       "   张二   0  85  16  40  64   1\n",
       "2班 张五  55  19  92  74  33  46\n",
       "   张六  29  17  31  55  47  53\n",
       "   张四  25  46  44   4  24  27"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.unstack()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "662bc575",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30a1b18c",
   "metadata": {},
   "source": [
    "- 聚合操作\n",
    "    - 求和\n",
    "    - 平均值\n",
    "    - 最大值\n",
    "    - 最小值"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
